# -*- coding: utf-8 -*-

import gtk
import webbrowser

class liligui(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)

        self.set_title("lilivro")
        self.resize(640, 480)
        self.connect("destroy", self.exit_app)


        main_vbox = gtk.VBox()
        self.add(main_vbox)

        accel_group = gtk.AccelGroup()
        self.add_accel_group(accel_group)
        
        # Menus
        file_menu = gtk.Menu()

        new_item = gtk.ImageMenuItem(gtk.STOCK_NEW)
        file_menu.append(new_item)
        new_item.connect('activate', self.menu_callbacks, 'FILE.NEW')
        new_item.add_accelerator("activate", accel_group, ord("n"),
                                  gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)

        
        load_item = gtk.ImageMenuItem(gtk.STOCK_OPEN)
        file_menu.append(load_item)
        load_item.connect('activate', self.menu_callbacks, 'FILE.OPEN')
        load_item.add_accelerator("activate", accel_group, ord("o"),
                                  gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)

        save_item = gtk.ImageMenuItem(gtk.STOCK_SAVE)
        file_menu.append(save_item)
        save_item.connect('activate', self.menu_callbacks, 'FILE.SAVE')
        save_item.add_accelerator("activate", accel_group, ord("s"),
                                  gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)

        save_as_item = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
        file_menu.append(save_as_item)
        save_as_item.connect('activate', self.menu_callbacks, 'FILE.SAVE_AS')

        file_menu.append( gtk.SeparatorMenuItem() )

        quit_item = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        file_menu.append(quit_item)
        quit_item.connect("activate", self.menu_callbacks, "FILE.QUIT")
        quit_item.add_accelerator("activate", accel_group, ord("q"),
                                  gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)


        create_menu = gtk.Menu()

        author_item = gtk.MenuItem("Novo autor")
        create_menu.append(author_item)
        author_item.connect('activate', self.menu_callbacks, 'CREATE.NEW_AUTHOR')

        
        help_menu = gtk.Menu()

        about_item = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
        help_menu.append(about_item)
        about_item.connect('activate', self.menu_callbacks, "HELP.ABOUT")


        mi_arquivo = gtk.MenuItem('Arquivo')
        mi_arquivo.set_submenu(file_menu)

        mi_create = gtk.MenuItem('Criar')
        mi_create.set_submenu(create_menu)
        
        mi_help = gtk.MenuItem('Ajuda')
        mi_help.set_submenu(help_menu)

        menubar = gtk.MenuBar()
        main_vbox.pack_start( menubar, False, False, 0 )

        menubar.add( mi_arquivo )
        menubar.add( mi_create )
        menubar.add( mi_help )
        # End Menus

        progressbar = gtk.ProgressBar()
        main_vbox.pack_end( progressbar, False, False, 0 )
        progressbar.show()

        hpaned = gtk.HPaned()
        main_vbox.pack_start(hpaned)

        # Seach area
        search_vbox = gtk.VBox()
        hpaned.add1( search_vbox )

        hbox = gtk.HBox()
        search_vbox.pack_start( hbox, False, False, 0 )
        
        search_entry = gtk.Entry()
        hbox.pack_start( search_entry, True, True, 0 )

        search_button = gtk.Button()
        hbox.pack_start( search_button, False, False, 0 )
        search_button.connect("clicked", self.button_callbacks, "SEARCH_BUTTON")
        image = gtk.Image()
        image.set_from_stock(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON)
        search_button.set_image( image )

        search_combobox = gtk.combo_box_new_text()
        search_vbox.pack_start( search_combobox, False, False, 0 )
        search_combobox.append_text('Autor')
        search_combobox.append_text('Editora')
        search_combobox.append_text('Título do Livro')
        search_combobox.set_active(0)

        search_treeview = gtk.TreeView()
        search_vbox.pack_start( search_treeview )


        # Info area
        info_vbox = gtk.VBox()
        hpaned.add2( info_vbox )

        hbox, self.book_title_entry = self.create_label_entry( 'Nome do livro: ' )
        info_vbox.pack_start(hbox, False, False, 0)

        hbox, self.book_author_entry = self.create_label_entry( 'Autor: ' )
        self.book_author_entry.set_editable( False )
        self.switch_autor_button = gtk.Button('Alterar')
        hbox.pack_end(self.switch_autor_button, False, False, 0 )
        info_vbox.pack_start(hbox, False, False)

        hbox = gtk.HBox()
        info_vbox.pack_start( hbox, False, False, 0)
        label = gtk.Label('Ano de lançamento: ')
        hbox.pack_start( label, False, False, 0)
        adjust = gtk.Adjustment( value=1983,
                                 upper=3000,
                                 step_incr=1 )                               
        self.release_year_spinbutton = gtk.SpinButton( adjustment=adjust )
        hbox.pack_start( self.release_year_spinbutton,
                         False, False, 0 )

        hbox=gtk.HBox()
        info_vbox.pack_start( hbox, False, False, 0)
        label = gtk.Label('Data da leitura: ')
        hbox.pack_start( label, False, False, 0 )
        calendar = gtk.Calendar()
        hbox.pack_start( calendar, False, False, 0 )

        hbox=gtk.HBox()
        info_vbox.pack_start( hbox, False, False, 0 )
        label = gtk.Label('Edição: ')
        hbox.pack_start( label, False, False, 0 ) 
        adjust = gtk.Adjustment( value=1,
                                 upper=100,
                                 lower=1,
                                 step_incr=1 )        
        self.edition_spinbutton = gtk.SpinButton( adjustment=adjust )
        hbox.pack_start( self.edition_spinbutton,
                         False, False, 0 )

        hbox=gtk.HBox()
        info_vbox.pack_start( hbox, False, False, 0 )
        label = gtk.Label('Idioma: ')
        hbox.pack_start( label, False, False, 0 )
        self.language_combobox = gtk.combo_box_new_text()
        hbox.pack_start( self.language_combobox,
                         False, False, 0 )
        self.language_combobox.append_text('Espanhol')
        self.language_combobox.append_text('Francês')
        self.language_combobox.append_text('Inglês')
        self.language_combobox.append_text('Italiano')
        self.language_combobox.append_text('Português')
        self.language_combobox.set_active(4)

        info_vbox.pack_start( gtk.Label('Descrição'), False, False, 0 )
        self.description_textbuffer = gtk.TextBuffer()
        self.description_textview = gtk.TextView( self.description_textbuffer )
        info_vbox.pack_start( self.description_textview,
                              True, True, 10)

        self.restore_button = gtk.Button('Restaurar')
        image = gtk.Image()
        image.set_from_stock(gtk.STOCK_UNDO,
                             gtk.ICON_SIZE_BUTTON)
        self.restore_button.set_image( image )
        self.save_button = gtk.Button('Salvar')
        image = gtk.Image()
        image.set_from_stock(gtk.STOCK_SAVE,
                             gtk.ICON_SIZE_BUTTON)
        self.save_button.set_image( image )
        button_pack = gtk.HButtonBox()
        button_pack.set_layout( gtk.BUTTONBOX_END)
        button_pack.set_spacing( 10 )
        info_vbox.pack_end( button_pack, False, False, 0 )
        button_pack.pack_end( self.restore_button )
        button_pack.pack_end( self.save_button )
        

        self.show_all()


    def create_label_entry( self, label_text ):
        hbox = gtk.HBox()
        label = gtk.Label(label_text)
        hbox.pack_start(label, False, False, 0)
        entry = gtk.Entry()
        hbox.pack_start(entry)

        return hbox, entry


    def menu_callbacks( self, menu, src ):        
        if src == 'FILE.NEW':
            pass
        elif src == 'FILE.OPEN':
            pass
        elif src == 'FILE.QUIT':
            self.exit_app()
        elif src == 'CREATE.NEW_AUTHOR':
            dia = gtk.Dialog("Novo author",
                             self,
                             gtk.DIALOG_MODAL,
                             (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                              gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))

            hbox, name_entry = self.create_label_entry('Nome: ')            
            dia.vbox.pack_start(hbox)

            hbox = gtk.HBox()

            vbox = gtk.VBox()
            vbox.pack_start(gtk.Label("Nascimento"))
            birthday = gtk.Calendar()
            vbox.pack_start(birthday)
            hbox.pack_start( vbox )

            vbox = gtk.VBox()
            vbox.pack_start(gtk.Label("Morte"))
            deathday = gtk.Calendar()
            vbox.pack_start(deathday)
            hbox.pack_start( vbox )

            dia.vbox.pack_start(hbox)

            dia.vbox.pack_start( gtk.Label('Descrição'), False, False, 0 )
            description_textbuffer = gtk.TextBuffer()
            description_textview = gtk.TextView( description_textbuffer )
            description_textview.set_wrap_mode( gtk.WRAP_WORD )
            sw = gtk.ScrolledWindow()
            sw.set_policy( gtk.POLICY_AUTOMATIC,
                           gtk.POLICY_AUTOMATIC )
            sw.add( description_textview )
            sw.show()
            dia.vbox.pack_start( sw,
                                 True, True, 10)
            
            
            dia.show_all()
            response = dia.run()

            if response == gtk.RESPONSE_ACCEPT:
                pass
                
            dia.destroy()
            
        elif src == 'HELP.ABOUT':
            gtk.about_dialog_set_url_hook( self.open_browser,
                                           'http://lilivros.googlecode.com' )

            dia = gtk.AboutDialog()

            dia.set_name( 'Lilivros' )
            dia.set_version( '0.1' )
            dia.set_copyright( 'GPL 2')
            dia.set_comments( 'A biblioteca privada da Lili.' )
            dia.set_website_label( 'Lilivros' )
            dia.set_website('http://lilivros.googlecode.com')
            dia.set_authors(['Diogo F. S. Ramos'])
            dia.set_program_name('Lilivros')


            dia.run()

            dia.destroy()
        else:
            print 'Menu desconhecido'


    def button_callbacks(self, button, src):
        if src == 'SEARCH_BUTTON':
            pass
        else:
            print 'Menu desconhecido'
            
        
    def run(self):
        gtk.main()


    def open_browser(self, dialog, url, data ):
        webbrowser.open(url)
        
        
    def exit_app( self, data=None ):

        gtk.main_quit()
